package com.yhp.dao.impl;

import com.yhp.bean.Menu;
import com.yhp.dao.MenuDao;
import com.yhp.util.Dbutils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class MenuDaoImpl extends Dbutils implements MenuDao {
    @Override
    public List<Menu> getMenuList() {
        List list = null;
        try {
            String sql = "SELECT * FROM menu";
            ResultSet query = query(sql, null);
            list = new ArrayList();
            while (query.next()){
                Menu menu = new Menu();
                menu.setMenuId(query.getInt("menuid"));
                menu.setMenuName(query.getString("menuname"));
                menu.setUpMenuId(query.getInt("upmenuid"));
                menu.setUrl(query.getString("url"));
                menu.setInDesc(query.getString("indesc"));
                menu.setState(query.getInt("state"));
                list.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return list;
    }

    @Override
    public List<Menu> getMenu(int index, int pageSize) {
        List<Menu> list = null;
        try {
            String sql = "SELECT * FROM menu limit ?,?";
            List param = new ArrayList();
            param.add((index-1)*pageSize);
            param.add(pageSize);
            ResultSet query = query(sql, param);
            list = new ArrayList<>();
            while (query.next()){
                Menu menu = new Menu();
                menu.setMenuId(query.getInt("menuid"));
                menu.setMenuName(query.getString("menuname"));
                menu.setUpMenuId(query.getInt("upmenuid"));
                menu.setUrl(query.getString("url"));
                menu.setInDesc(query.getString("indesc"));
                menu.setState(query.getInt("state"));
                list.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return list;
    }

    @Override
    public int total() {
        int num = 0;
        try {
            num = 0;
            String sql = "SELECT COUNT(*) FROM MENU";
            ResultSet query = query(sql, null);
            while (query.next()){
                num=query.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return num;
    }

    @Override
    public int addMenu(Menu menu) {
        int update = 0;
        try {
            String sql = "insert into menu values(null,?,?,?,?,?)";
            List param = new ArrayList();
            param.add(menu.getMenuName());
            param.add(menu.getUpMenuId());
            param.add(menu.getState());
            param.add(menu.getInDesc());
            param.add(menu.getUrl());
            update = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return update;
    }

    @Override
    public int deleteMenu(int id) {
        int update = 0;
        try {
            String sql = "delete from menu where menuid=?";
            List param = new ArrayList();
            param.add(id);
            update = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return update;
    }

    @Override
    public Menu findById(int id) {
        Menu menu = null;
        try {
            String sql = "SELECT * FROM menu where menuid=?";
            List param = new ArrayList();
            param.add(id);
            ResultSet query = query(sql, param);
             menu= new Menu();
            while (query.next()){
                menu.setMenuId(query.getInt("menuid"));
                menu.setMenuName(query.getString("menuname"));
                menu.setUpMenuId(query.getInt("upmenuid"));
                menu.setUrl(query.getString("url"));
                menu.setInDesc(query.getString("indesc"));
                menu.setState(query.getInt("state"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return menu;
    }

    @Override
    public int updateMenu(Menu menu) {
        int update = 0;
        try {
            String sql = "update menu set menuName=?,upmenuid=?,state=?,indesc=?,url=? where menuid=?";
            List param = new ArrayList();
            param.add(menu.getMenuName());
            param.add(menu.getUpMenuId());
            param.add(menu.getState());
            param.add(menu.getInDesc());
            param.add(menu.getUrl());
            param.add(menu.getMenuId());
            update = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return update;
    }
}
